const {
    coupon
} = require("../../../utils/api")
import Toast from "../../../vant/weapp/toast/toast";
import clickreply from "../../../utils/clickreply";
// pages/index/order/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        balance: true,
        wxpay: true,
        couponlist: true,
        pricecouponlist: true,
        cardcouponlist: true,
        orderDetail: [],
        add_time: '',
        orderType: 0,
        store_id: 0,
        fz: 0,
        coupon: {
            id: ''
        },
        pricecoupon: {
            id: ''
        },
        cardcoupon: {
            id: ''
        },
        order_on: '',
        from: true,
        counting: false,
        disabled: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.setStorageSync('coupon', '')
        wx.setStorageSync('pricecoupon', '')
        wx.setStorageSync('cardcoupon', '')
        console.log(options.store_id)
        const that = this
        app.globalData.isUnload = false
        if (options.from === "renew") {
            console.log(options.from)
            that.getrenewalOrderInfo({
                order_on: options.order_on,
            })
            that.setData({
                pricecouponlist: false,
                cardcouponlist: false,
                from: false,
            })
        } else {
            that.setData({
                order_on: options.order_on,
                store_id: options.store_id,
            })
            that.getOrderDetail({
                store_id: options.store_id,
                order_on: options.order_on
            })
        }
        that.countDown(that, 120);
    },
    countDown(that, count) {
        //  console.log(count)
        if (count == 0) {
            that.setData({
                disabled: 'disabled',
                timeCountDownTop: '请重新选择时间下单',
                counting: false
            })
            return;
        }

        that.setData({
            counting: true,
            timeCountDownTop: '支付倒计时' + count + '秒',
        })

        setTimeout(function () {
            count--;
            that.countDown(that, count);
        }, 1000);
    },
    onShow() {
        this.setData({
            coupon: wx.getStorageSync('coupon'),
            cardcoupon: wx.getStorageSync('cardcoupon'),
            pricecoupon: wx.getStorageSync('pricecoupon')
        })

        this.setData({
            orderType: 0,
            balance: true,
            wxpay: true,
            couponlist: true,
            pricecouponlist: this.data.from ? true : false,
            cardcouponlist: this.data.from ? true : false,
        })
        console.log(wx.getStorageSync('coupon'))
        console.log(wx.getStorageSync('pricecoupon'))
        if (wx.getStorageSync('coupon') && wx.getStorageSync('pricecoupon') == '') {
            console.log('优惠卷')
            if (this.data.coupon.price - this.data.orderDetail.order.price < 0) {
                console.log(this.data.coupon.price - this.data.orderDetail.order.price)
                this.setData({
                    orderType: 0,
                    balance: false,
                    wxpay: true,
                    pricecouponlist: false,
                    cardcouponlist: false
                })
            } else {
                this.setData({
                    orderType: 0,
                    balance: true,
                    wxpay: false,
                    cardcouponlist: false,
                    pricecouponlist: false
                })
            }


        }

        if (wx.getStorageSync('pricecoupon')) {
            console.log('团购卷')
            this.setData({
                pricecouponid: this.data.pricecoupon.id,
                orderType: 5,
                couponlist: false,
                cardcouponlist: false,
                balance: false,
                wxpay: false
            })
        }


        if (wx.getStorageSync('cardcoupon')) {
            console.log('团购卷')
            this.setData({
                cardcouponid: this.data.cardcoupon.id,
                orderType: 6,
                couponlist: false,
                pricecouponlist: false,
                balance: false,
                wxpay: false
            })
        }

    },
    onUnload() {
        const that = this
        console.log(`onUnload - MakeDelete1`, app.globalData.isUnload)
        if (app.globalData.isUnload) {
            // 页面手动跳转出去
            return
        } else {
            // 其他跳出方式 (back键)
            app.http.post(app.api.Delete, {
                order_on: that.data.order_on
            }, (res) => {
                wx.removeStorageSync('coupon')
            })
            // 是否需要删除订单
        }
    },
    // 包厢预定详情
    getOrderDetail(options) {
        console.log(options)
        const that = this
        app.http.post(app.api.OrderDetail, {
            order_on: options.order_on
        }, (res) => {
            let add_time = res.data.order.add_time.slice(0, 16)
            that.setData({
                orderDetail: res.data,
                fz: res.data.fz,
                add_time,
                store_id: options.store_id,
                order_on: options.order_on
            })
        })
    },
    // 续单预定详情
    getrenewalOrderInfo(options) {
        const that = this
        console.log(options)
        app.http.post(app.api.renewalOrderInfo, {
            order_on: options.order_on
        }, (res) => {
            console.log(96, res)
            let add_time = res.data.order.add_time.slice(0, 16)
            that.setData({
                fz: res.data.fz,
                orderDetail: res.data,
                add_time,
                store_id: res.data.order.store_id,
                order_on: options.order_on
            })
        })
    },
    getWaChat(e) {
        const that = this
        console.log(that.data.fz)
        wx.login({
            success(codes) {
                wx.hideLoading()
                if (codes.code) {
                    app.http.post(that.data.fz ? app.api.wechatfz : app.api.wechat, {
                        order_on: e.order_on,
                        code: codes.code,
                        type: e.type
                    }, (re) => {
                        wx.requestPayment({
                            appid: re.data.appId,
                            timeStamp: re.data.timeStamp,
                            nonceStr: re.data.nonceStr,
                            package: re.data.package,
                            signType: re.data.signType,
                            paySign: re.data.paySign,
                            success(da) {
                                if (da.errMsg == 'requestPayment:ok') {
                                    wx.showModal({
                                        title: '成功',
                                        content: "预定成功",
                                        showCancel: false,
                                        complete: (res) => {
                                            if (res.confirm) {
                                                wx.reLaunch({
                                                    url: '/pages/order/index?status=1',
                                                })
                                            }
                                        }
                                    })

                                }
                            },
                            fail() {
                                if (!that.data.from) {
                                    return
                                }
                                app.globalData.isUnload = false
                                Toast({
                                    type: 'fail',
                                    message: '预定失败',
                                    onClose: () => {

                                    },
                                });
                            }
                        })
                    })

                }
            }
        })
    },
    // 包厢预定支付
    pay1() {
        if (clickreply.preventDuplicateClicks(1)) return;
        // wx.showLoading({
        //     mask:true
        //   })
        const that = this
        console.log(80, that.data.coupon.id)
        let coupon_id = ''
        if (that.data.orderType == 0) {
            Toast.fail("请选择支付方式")
            return
        }
        // 优惠券
        if (that.data.orderType == 5) {
            coupon_id = that.data.pricecouponid
        }else if (that.data.orderType == 6) {
            coupon_id = that.data.cardcouponid
        }else {
            if (that.data.coupon.id) {
                coupon_id = that.data.coupon.id
            }
        }

        app.http.post(app.api.addOrder, {
            order_on: that.data.order_on,
            type: that.data.orderType,
            coupon_id
        }, (res) => {
            app.globalData.isUnload = true
            if (that.data.orderType == 2) {
                wx.hideLoading()
                wx.showModal({
                    title: '成功',
                    content: res.message,
                    showCancel: false,
                    complete: (res) => {
                        if (res.confirm) {
                            wx.reLaunch({
                                url: '/pages/order/index',
                            })
                        }
                    }
                })
            } else if (that.data.orderType == 5) {
                wx.hideLoading()
                wx.showModal({
                    title: '成功',
                    content: res.message,
                    showCancel: false,
                    complete: (res) => {
                        if (res.confirm) {
                            wx.reLaunch({
                                url: '/pages/order/index',
                            })
                        }
                    }
                })
            }else if (that.data.orderType == 6) {
                wx.hideLoading()
                wx.showModal({
                    title: '成功',
                    content: res.message,
                    showCancel: false,
                    complete: (res) => {
                        if (res.confirm) {
                            wx.reLaunch({
                                url: '/pages/order/index',
                            })
                        }
                    }
                })
            } else {
                that.getWaChat({
                    order_on: res.data.order_on,
                    type: 3
                })
                wx.hideLoading()
                wx.showModal({
                    title: '成功',
                    content: res.message,
                    showCancel: false,
                    complete: (res) => {
                        if (res.confirm) {
                            wx.reLaunch({
                                url: '/pages/order/index',
                            })
                        }
                    }
                })
            }
            wx.removeStorageSync('coupon')
        })
    },
    // 续单预定支付
    pay2() {
        if (clickreply.preventDuplicateClicks(2)) return;
        // wx.showLoading({
        //     mask:true
        //   })
        const that = this
        let coupon_id = ''
        if (that.data.orderType == 0) {
            Toast.fail("请选择支付方式")
            return
        }
        // 优惠券
        if (that.data.coupon.id) {
            coupon_id = that.data.coupon.id
        }
        app.http.post(app.api.renewalAddorder, {
            order_on: that.data.order_on,
            type: that.data.orderType,
            coupon_id
        }, (res) => {
            app.globalData.isUnload = true
            if (that.data.orderType == 2) {
                wx.hideLoading()
                wx.showModal({
                    title: '成功',
                    content: res.message,
                    showCancel: false,
                    complete: (res) => {
                        if (res.confirm) {
                            wx.reLaunch({
                                url: '/pages/order/index',
                            })
                        }
                    }
                })
            } else {
                that.getWaChat({

                    order_on: res.data.order_on,
                    type: 7
                })
            }

            wx.removeStorageSync('coupon')
        })
    },
    toCoupon() {
        const that = this
        console.log(that.data.store_id)
        wx.navigateTo({
            url: '/pages/person/maincoupon/index?from=roomOrder&store_id=' + this.data.store_id,
        })
    },
    toPriceCoupon() {
        const that = this
        console.log(that.data.from)
        let xd = that.data.from ? 0 : 1
        wx.navigateTo({
            url: '/pages/person/mainpricecoupon/index?from=roomOrder&store_id=' + this.data.store_id + '&order_on=' + this.data.orderDetail.order.order_on + '&xd=' + xd,
        })
    },
    tocardCoupon() {
        const that = this
        console.log(that.data.from)
        let xd = that.data.from ? 0 : 1
        wx.navigateTo({
            url: '/pages/person/cardcoupon/index?from=roomOrder&store_id=' + this.data.store_id + '&order_on=' + this.data.orderDetail.order.order_on + '&xd=' + xd,
        })
    },
    choosType(e) {
        if (e.currentTarget.dataset.type == 2) {
            if (this.data.coupon.price < this.data.orderDetail.order.price) {
                this.setData({
                    couponlist: false
                })
            }

        }
        if (e.currentTarget.dataset.type == 1) {
            this.setData({
                couponlist: true
            })
        }
        this.setData({
            orderType: e.currentTarget.dataset.type
        })
    }
})